package com.foreknow.elm.controller;

import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CardService;
import com.foreknow.elm.service.impl.CardServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.smartcardio.Card;
import java.util.List;

public class CartController {
    CardService cardService = new CardServiceImpl();
    public List<Cart> listCart(HttpServletRequest request)throws Exception{
        String uid = request.getParameter("userId");
        Integer bid = 0;
        if (request.getParameter("businessId")!=null){
            bid = Integer.parseInt(request.getParameter("businessId"));
        }
        Cart car = new Cart();
        car.setUserId(uid);
        if (request.getParameter("businessId")!=null){
            car.setBusinessId(bid);
        }
        List<Cart> list = cardService.squery(car);
        return list;
    }

    public int saveCart(HttpServletRequest request)throws Exception{
        String uid = request.getParameter("userId");
        String bid = request.getParameter("businessId");
        int bbid = Integer.parseInt(bid);
        String fid = request.getParameter("foodId");
        int ffid = Integer.parseInt(fid);
        Cart cart = new Cart();
        cart.setUserId(uid);
        cart.setBusinessId(bbid);
        cart.setFoodID(ffid);
        int a = cardService.sinsert(cart);
        return a;
    }

    public int updateCart(HttpServletRequest request)throws Exception{
        String uid = request.getParameter("userId");
        String bid = request.getParameter("businessId");
        int bbid = Integer.parseInt(bid);
        String fid = request.getParameter("foodId");
        int ffid = Integer.parseInt(fid);
        String quant = request.getParameter("quantity");
        int qua = Integer.parseInt(quant);
        Cart cart = new Cart();
        cart.setUserId(uid);
        cart.setBusinessId(bbid);
        cart.setFoodID(ffid);
        cart.setQuantity(qua);
        int a = cardService.supdatequa(cart);
        return a;
    }

    public int removeCart(HttpServletRequest request)throws Exception{
        String uid = request.getParameter("userId");
        String bid = request.getParameter("businessId");
        int bbid = Integer.parseInt(bid);
        int ffid = 0;
        if (request.getParameter("foodId")!=null){
            String fid = request.getParameter("foodId");
            ffid = Integer.parseInt(fid);
        }
        Cart cart = new Cart();
        cart.setUserId(uid);
        cart.setBusinessId(bbid);
        if (request.getParameter("foodId")!=null){
            cart.setFoodID(ffid);
        }
        int a = cardService.sdelete(cart);
        return a;
    }
}
